//
//  ZDMessageController.m
//  特种车调度
//
//  Created by 陈宇 on 15/8/18.
//
//

#import "ZDMessageController.h"
#import "BMNoHilightButton.h"
#import "ZDNoticeCell.h"
#import "BMEmptyView.h"
#import "ZDOrderDetailController.h"
#import "BMWebController.h"
#import "NoticeManager.h"
//#import "CZOrder.h"


@interface ZDMessageController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NoticeManager *manager;
@property (nonatomic, strong) NSMutableArray *list1;
@property (nonatomic, strong) NSMutableArray *list2;
@property (nonatomic, weak) UITableView *msgView;
@property (nonatomic, weak) UITableView *noticeView;

@property (weak, nonatomic) IBOutlet BMNoHilightButton *msgButton;
@property (weak, nonatomic) IBOutlet BMNoHilightButton *noticeButton;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *indicatorLeftMargin;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@property (nonatomic, strong) BMEmptyView *msgEmptyView;
@property (nonatomic, strong) BMEmptyView *noticeEmptyView;

@property (nonatomic, weak) UIButton *currentButton;

- (IBAction)menuClick:(UIButton *)sender;

@end

@implementation ZDMessageController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"消息中心";
    self.scrollView.scrollEnabled = NO;
    
    [self configuration];
    
    self.list1 = [NSMutableArray array];
    self.list2 = [NSMutableArray array];
    self.manager = [NoticeManager new];
}

- (void)loadMsgsReset:(BOOL)reset
{
    if (reset) {
        [self.manager resetWithType:1];
    }
    
    [_manager getMessageComplete:^(BMResponse *response, NSError *error) {
        if ([_msgView.header isRefreshing]) {
            [_msgView.header endRefreshing];
        }
        if ([_msgView.footer isRefreshing]) {
            [_msgView.footer endRefreshing];
        }
        if (response.status == kResultOK) {
            if (reset) {
                [self.list1 removeAllObjects];
            }
            [self.list1 addObjectsFromArray:response.result];
            [_msgView reloadData];
        } else {
            //            [self showHint:@"加载失败!"];
        }
    }];
}

- (void)loadNoticesReset:(BOOL)reset
{
    [self.manager resetWithType:2];
    [_manager getNoticeComplete:^(BMResponse *response, NSError *error) {
        if ([_noticeView.header isRefreshing]) {
            [_noticeView.header endRefreshing];
        }
        if ([_noticeView.footer isRefreshing]) {
            [_noticeView.footer endRefreshing];
        }
        if (response.status == kResultOK) {
            if (reset) {
                [self.list2 removeAllObjects];
            }
            [self.list2 addObjectsFromArray:response.result];
            [_noticeView reloadData];
        } else {
            //            [self showHint:@"加载失败!"];
        }
    }];
}

- (void)configuration {
    for (int i = 1; i < 3; i++) {
        UITableView *tableView = [[UITableView alloc] init];
        tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [tableView registerNib:[UINib nibWithNibName:@"ZDNoticeCell" bundle:nil] forCellReuseIdentifier:reuseIdentifier];
        tableView.backgroundColor = [UIColor clearColor];
        tableView.tableFooterView = [[UIView alloc] init];
        tableView.rowHeight = UITableViewAutomaticDimension;
        tableView.estimatedRowHeight = 100.f;
        tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            if (i == 1) {
                [self loadMsgsReset:YES];
            } else {
                [self loadNoticesReset:YES];
            }
        }];
        tableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            if (i == 1) {
                [self loadMsgsReset:NO];
            } else {
                [self loadNoticesReset:NO];
            }
        }];
        tableView.footer.automaticallyHidden = YES;
        tableView.tag = i;
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.frame = _scrollView.bounds;
        tableView.x = (i - 1) * self.view.width;
        [_scrollView addSubview:tableView];
        if (i == 1) {
            _msgView = tableView;
        } else {
            _noticeView = tableView;
        }
        [tableView.header beginRefreshing];
    }
    _scrollView.contentSize = CGSizeMake(kScreenWidth * 2, 0);
}

- (BMEmptyView *)msgEmptyView
{
    if (!_msgEmptyView) {
        _msgEmptyView = [[BMEmptyView alloc] init];
        _msgEmptyView.message = @"暂无消息哦!";
        _msgEmptyView.image = [UIImage imageNamed:@"empty_msg"];
    }
    return _msgEmptyView;
}

- (BMEmptyView *)noticeEmptyView
{
    if (!_noticeEmptyView) {
        _noticeEmptyView = [[BMEmptyView alloc] init];
        _noticeEmptyView.message = @"暂无公告哦!";
        _noticeEmptyView.image = [UIImage imageNamed:@"empty_msg"];
    }
    return _noticeEmptyView;
}

- (IBAction)menuClick:(UIButton *)sender {
    
    if (_currentButton == sender) return;
    
    [_scrollView setContentOffset:CGPointMake(_scrollView.width * sender.tag, 0) animated:YES];
    NSLog(@"22222222222");
}

#pragma mark - UIScrollViewDelegate代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == _scrollView) {
        _indicatorLeftMargin.constant = scrollView.contentOffset.x * 0.5;
        BOOL no = scrollView.contentOffset.x / self.scrollView.width + 0.5 > 1.f;
        _noticeButton.selected = no;
        _msgButton.selected = !no;
        _currentButton = no ? _noticeButton : _msgButton;
    }
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger count = 0;
    UIView *targetView = nil;
    
    switch (tableView.tag) {
        case 1:
            count = _list1.count;
            targetView = self.msgEmptyView;
            break;
        case 2:
            count = _list2.count;
            targetView = self.noticeEmptyView;
            break;
    }
    
    if (!count) {
        targetView.frame = tableView.bounds;
        [tableView addSubview:targetView];
    } else if (count && targetView.superview) {
        [targetView removeFromSuperview];
    }
    return count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZDNoticeCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    Notice *notice;
    switch (tableView.tag) {
        case 1:
            notice = _list1[indexPath.row];
            break;
        case 2:
            notice = _list2[indexPath.row];
            break;
    }
    cell.notice = notice;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 1) {
        Notice *notice = _list1[indexPath.row];
        if (notice.type == 1) {
            ZDOrderDetailController *detailVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"orderDetailVC"];
//            CZOrder *order = [CZOrder new];
//            order.ID = notice.typeId;
//            detailVC.order = order;
            [self showViewController:detailVC sender:nil];
        } else {

        }
    } else {
//        Notice *notice = _list2[indexPath.row];
        BMWebController *webVC = [BMWebController new];
        webVC.path = [NSURL URLWithString:@"http://www.baidu.com"];
        [self showViewController:webVC sender:nil];
    }
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        if (tableView.tag == 1) {
            [self deleteMsgTask:indexPath];
        } else {
            [self deleteNoticeTask:indexPath];
        }
    }
}

- (void)deleteMsgTask:(NSIndexPath *)indexpath
{
    [self showHudWithhint:kDefaultMessage];
    [self.manager deleteMsg:_list1[indexpath.row] complete:^(BMResponse *response, NSError *error) {
        [self hideHud];
        if (response.status == kResultOK) {
            [self.list1 removeObjectAtIndex:indexpath.row];
            [self.msgView deleteRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationNone];
        } else {
            [self showHint:@"删除失败!"];
        }
    }];
}

- (void)deleteNoticeTask:(NSIndexPath *)indexpath
{
    [self showHudWithhint:kDefaultMessage];
    [self.manager deleteNotice:_list2[indexpath.row] complete:^(BMResponse *response, NSError *error) {
        [self hideHud];
        if (response.status == kResultOK) {
            [self.list2 removeObjectAtIndex:indexpath.row];
            [self.noticeView deleteRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationNone];
        } else {
            [self showHint:@"删除失败!"];
        }
    }];
}

@end
